﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using System.Windows.Controls;
using System.Collections.Generic;

namespace WPFExcelReport
{
    public partial class TAP04COND
    {
        public TAP04COND()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VAPFOLGE();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {

            TBMain.Clear();
            dwData = TMV.TBMain.NewRow();
            dwData["APFMANDANT"] = app.Mandant;
            dwData["APFANRKRS"] = " AND APFANRKRS LIKE '" + KRS + "%'";
            dwData["APFALFDNR"] = " AND APFALFDNR LIKE '" + LFD + "%'";
            SResult = TMV.QAPF09(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }
        public override void OnEdit()
        {
            NRi = 1;
            base.OnEdit();
        }
        int mpos = 0;
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsNew)
                {
                    IsNew = true;
                    POS = "";
                    return;
                }
                if (POS == "000" || POS == "")
                {
                    throw new Exception("必需手工输入步骤编号");
                }
                mpos++;
                if (POS.Trim().Length == 0 || POS == "000")
                {
                    POS = Helpers.Helper.Right("000" + (mpos * 10).ToString(), 3);
                }
                PM["NRKRS"] = KRS;
                PM["LFDNR"] = LFD;
                PM["FOLGE"] = POS;
                PM["DES"] = DES;
                TMV.NEWAPP(PM);
                POS = "";
                IsNew = false;
                vwMaster.View.MoveCurrentToFirst();
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DAPF01(dwMaster);
            //APFMANDANT=:APFMANDANT: AND APFANRKRS=:APFANRKRS: AND APFALFDNR =:APFALFDNR: AND APFAFOLGE=:APFAFOLGE: 
            filter = "APFANRKRS='" + dwMaster["APFANRKRS"].ToString() + "' AND APFALFDNR='"
                + dwMaster["APFALFDNR"].ToString() + "' AND APFAFOLGE='" + dwMaster["APFAFOLGE"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {

                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
         
        public override void OnEXAUFChg(string vlaue)
        {
            if (!IsCanLoad)
                return;
            if (EXAUF.Length < 8)
                return;
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 6);
            dwData = TBMain.NewRow();
            dwData["APFMANDANT"] = app.Mandant;
            dwData["APFANRKRS"] = " AND APFANRKRS ='" + EXAUF.Substring(0, 2) + "'";
            dwData["APFALFDNR"] = " AND APFALFDNR = '" + EXAUF.Substring(2, 6) + "'";
            SResult = TMV.QAPF09(dwData);
            MergeData(SResult, false);
            if (TBMain.Rows.Count > 0)
            {
                var gapos = (from DataRow row in TBMain.AsEnumerable()
                             select int.Parse(row["APFAFOLGE"].ToString()))
                               .Max();
                mpos = Convert.ToInt16(gapos / 10);
            }
            else
            {
                mpos = 0;
            }
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count > 0)
                return;
            if (QTY == 0)
            {

                mpos++;
                POS = Helpers.Helper.Right("000" + (mpos * 10).ToString(), 3);

                PM["NRKRS"] = KRS;
                PM["LFDNR"] = LFD;
                PM["FOLGE"] = POS;
                PM["DES"] = DES;
                TMV.NEWAPP(PM);
            }
            else
            {
                while (mpos < QTY)
                {
                    mpos++;
                    POS = Helpers.Helper.Left((mpos).ToString() + "000", 3);
                    PM["NRKRS"] = KRS;
                    PM["LFDNR"] = LFD;
                    PM["FOLGE"] = POS;
                    PM["DES"] = DES;
                    TMV.NEWAPP(PM);
                }
            }

            base.OnEXAUFChg(vlaue);
        }
       
    }
}
